package com.helper.dream.a361phonehelper.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.helper.dream.a361phonehelper.R;
import com.helper.dream.a361phonehelper.bean.TuijianFenleiEntity;
import com.helper.dream.a361phonehelper.inter.ITuijianBodyInterface;
import com.helper.dream.a361phonehelper.utils.ApiManager;
import com.helper.dream.a361phonehelper.utils.HttpClientUtil;
import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by LamYu on 16/12/19/0019.
 */

public class TuijianFenleiFragment extends Fragment {
    View view, itemView,itemViewfoot;
    LinearLayout ll1, ll2,ll3,ll4;
    List<TuijianFenleiEntity.DataBean.HotTagsBean> dataHot = new ArrayList<>();
    List<TuijianFenleiEntity.DataBean.SoftTagsBean> dataSoft=new ArrayList<>();
    List<TuijianFenleiEntity.DataBean.GameTagsBean> dataGame=new ArrayList<>();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getData();
    }

    private void getData() {
        OkHttpClient client = HttpClientUtil.initClient();
        Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create())
                    .client(client)
                    .baseUrl(ApiManager.TjBodyBase).build();
            ITuijianBodyInterface tuijianBodyInterface = retrofit.create(ITuijianBodyInterface.class);
            Call<TuijianFenleiEntity> call = tuijianBodyInterface.getFenleiEntity();
            call.enqueue(new Callback<TuijianFenleiEntity>() {
                @Override
                public void onResponse(Call<TuijianFenleiEntity> call, Response<TuijianFenleiEntity> response) {
                    TuijianFenleiEntity body = response.body();
                    dataHot = body.getData().getHotTags();
                    dataSoft=body.getData().getSoftTags();
                    dataGame=body.getData().getGameTags();
                }

                @Override
                public void onFailure(Call<TuijianFenleiEntity> call, Throwable t) {

                }
            });
        }

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            view = inflater.inflate(R.layout.fragment_tuijian_fenlei, container, false);
            return view;
        }

        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            ll1 = (LinearLayout) view.findViewById(R.id.ll_tj_fl1);
            ll2 = (LinearLayout) view.findViewById(R.id.ll_tj_fl2);
            ll3= (LinearLayout) view.findViewById(R.id.ll_tj_fl3);
            ll4= (LinearLayout) view.findViewById(R.id.ll_tj_fl4);
            for (int i = 0; i < dataHot.size() / 2; i++) {
                itemView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_tuijian_fenlei_bodyitem, ll1, false);
                ImageView iv = (ImageView) itemView.findViewById(R.id.iv_tj_fl_bi);
                Glide.with(getActivity()).load(dataHot.get(i).getLogo()).into(iv);
                TextView tv = (TextView) itemView.findViewById(R.id.tv_tj_fl_bi);
                tv.setText(dataHot.get(i).getTitle());
                ll1.addView(itemView);
            }
            for (int i = dataHot.size() / 2; i < dataHot.size(); i++) {
                itemView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_tuijian_fenlei_bodyitem, ll2, false);
                ImageView iv = (ImageView) itemView.findViewById(R.id.iv_tj_fl_bi);
                Glide.with(getActivity()).load(dataHot.get(i).getLogo()).into(iv);
                TextView tv = (TextView) itemView.findViewById(R.id.tv_tj_fl_bi);
                tv.setText(dataHot.get(i).getTitle());
                ll2.addView(itemView);
            }
            for (int i = 0; i < dataSoft.size(); i++) {
                itemViewfoot=LayoutInflater.from(getActivity()).inflate(R.layout.fragment_tuijian_fenlei_footitem,ll3,false);
                ImageView iv= (ImageView) itemViewfoot.findViewById(R.id.iv_tj_fl_fi);
                Glide.with(getActivity()).load(dataSoft.get(i).getLogo()).into(iv);
                TextView tv= (TextView) itemViewfoot.findViewById(R.id.tv_tj_fl_fi);
                tv.setText(dataSoft.get(i).getTitle());
                ll3.addView(itemViewfoot);
            }
            for (int i = 0; i < dataGame.size(); i++) {
                itemViewfoot=LayoutInflater.from(getActivity()).inflate(R.layout.fragment_tuijian_fenlei_footitem,ll4,false);
                ImageView iv= (ImageView) itemViewfoot.findViewById(R.id.iv_tj_fl_fi);
                Glide.with(getActivity()).load(dataGame.get(i).getLogo()).into(iv);
                TextView tv= (TextView) itemViewfoot.findViewById(R.id.tv_tj_fl_fi);
                tv.setText(dataGame.get(i).getTitle());
                ll4.addView(itemViewfoot);
            }
        }
    }
